package com.tanhua.admin.controller;

import com.alibaba.cloud.commons.lang.StringUtils;
import com.tanhua.admin.service.ManageService;
import com.tanhua.model.db.UserInfo;
import com.tanhua.model.vo.MovementsVo;
import com.tanhua.model.vo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/manage")
public class ManageController {

    @Autowired
    private ManageService manageService;

    /**
     * 分页查询探花系统的所有用户信息
     *  GET  /users
     *  参数：
     *      pagesize，page
     *  返回值：
     *      PageResult  -- List<UserInfo>
     */
    @GetMapping("/users")
    public ResponseEntity users(@RequestParam(defaultValue = "1") Integer page,
                                @RequestParam(defaultValue = "10") Integer pagesize) {
        //1、调用service查询，
        PageResult vo = manageService.users(page,pagesize);
        //2、返回
        return ResponseEntity.ok(vo);
    }

    /**
     * 后台查询用户基本资料
     *   GET    /users/:userID
     */
    @GetMapping("/users/{userId}")
    public ResponseEntity findUserById(@PathVariable("userId") Long userId) {
        UserInfo info = manageService.findUserById(userId);
        return ResponseEntity.ok(info);
    }

    /**
     * 查询用户的视频列表
     *   GET    /videos
     */
    @GetMapping("/videos")
    public ResponseEntity findVideos(@RequestParam(defaultValue = "1") Integer page,
                                     @RequestParam(defaultValue = "10") Integer pagesize,
                                     Long uid) {
        PageResult vo = manageService.findVideos(uid,page,pagesize);
        return ResponseEntity.ok(vo);
    }

    /**
     * 分页查询动态列表
     *   get  /messages
     */
    @GetMapping("/messages")
    public ResponseEntity findMovements(@RequestParam(defaultValue = "1") Integer page,
                                        @RequestParam(defaultValue = "10") Integer pagesize,
                                        Long uid, String state) {
        //页面传入java代码中state参数有问题。
        Integer status = null;
        if(!StringUtils.isEmpty(state) && !"''".equals(state)) {
            status = Integer.valueOf(state);
        }
        PageResult vo = manageService.findMovements(status,uid,page,pagesize);
        return ResponseEntity.ok(vo);
    }

    /**
     * 查看动态详情
     *   get  /manage/messages/:id
     */
    @GetMapping("/messages/{movementId}")
    public ResponseEntity findMovementById(@PathVariable("movementId") String movementId){
        MovementsVo vo= manageService.findMovementById(movementId);
        return ResponseEntity.ok(vo);
    }

    /**
     * 查看评论列表翻页
     *   get  /manage/messages/comments
     */
    @GetMapping("/messages/comments")
    public ResponseEntity movementComments(@RequestParam(defaultValue = "1") Integer page,
                                           @RequestParam(defaultValue = "10") Integer pagesize,
                                           String messageID) {
        PageResult vo = manageService.movementComments(page,pagesize,messageID);
        return ResponseEntity.ok(vo);
    }

    /**
     * 动态通过
     * @param items
     * @return
     */
    @PostMapping("/messages/pass")
    public ResponseEntity pass(@RequestBody String[] items){
        //将获取到的动态Id数组交给service层处理并返回message信息
        String message=  manageService.pass(items);
        //封装map集合并返回
        Map map=new HashMap();
        map.put("message",message);
        return ResponseEntity.ok(map);
    }

    /**
     * 动态拒绝
     * @param items
     * @return
     */
    @PostMapping("/messages/reject")
    public ResponseEntity reject(@RequestBody String[] items){
        //将获取到的动态Id数组交给service层处理并返回message信息
        String message=  manageService.reject(items);
        //封装map集合并返回
        Map map=new HashMap();
        map.put("message",message);
        return ResponseEntity.ok(map);
    }

    //冻结用户
    @PostMapping("/users/freeze")
    public ResponseEntity freeze(@RequestBody Map params) {
        Map map = manageService.freeze(params);
        return ResponseEntity.ok(map);
    }

    //解冻用户
    @PostMapping("/users/unfreeze")
    public ResponseEntity unfreeze(@RequestBody Map params) {
        Map map = manageService.unfreeze(params);
        return ResponseEntity.ok(map);
    }
}
